/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.dao.implement;

import ar.com.gruposicom.dao.TrueValueDao;
import ar.com.gruposicom.domain.TrueValue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author marcelo
 */
public class TrueValueDaoImp extends DataManager implements TrueValueDao 
{

    private Statement stmt;
    private Connection conn;
    
    @Override
    public void save(TrueValue t, int idPolitica) 
    {
        try 
        { 
            conn = super.getConection();
            stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO valores_correctos (politica_id, valor, tipo_dato, regla_comparacion) VALUES ("
            +idPolitica+",'"+t.getValue()+"','"+t.getDataType()+"', '"+t.getRuleComparation()+"')");  
//            resu = stmt.executeQuery("Select Max(politica_id) From politicas");
            this.cerrar();
        }
        catch (SQLException ex)
        {
          while (ex !=null)
            {
            ex.printStackTrace();
            ex = ex.getNextException();
            }
        }
    }

    @Override
    public Collection<TrueValue> getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public TrueValue get(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(TrueValue t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void modify(TrueValue t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private void cerrar()
    {
      try
      {
        stmt.close();
        conn.close();
      }
      catch(SQLException e)
      {
        while (e != null) 
        {
          e.printStackTrace();
          e = e.getNextException();
        }        
      }      
    }

    @Override
    public Collection<TrueValue> getAllFromPolitic(int idPolitic) 
    {
        ResultSet resu = null;
        try 
        { 
            conn = super.getConection();      
            stmt = conn.createStatement();  
            resu = stmt.executeQuery("SELECT valores_id, politica_id, valor, tipo_dato, regla_comparacion FROM valores_correctos "
                    + "WHERE politica_id = "+idPolitic); 
        }
        catch (SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        } 
        Collection<TrueValue> co = new ArrayList<>();   
        try
        {
            while (resu.next())
            {
                TrueValue t = new TrueValue();
                t.setId(resu.getInt(1));
                t.setValue(resu.getString(3));
                t.setDataType(resu.getString(4));
                t.setRuleComparation(resu.getString(5));
                co.add(t);
            }
        }
        catch(SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
        this.cerrar();            
        return co; 
    }
    
}
